
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;
import javax.imageio.ImageIO;

/**
 * This class aims to reduce the excessive cost of image IO by
 * loading all the images for only one time right at the beginning of launching;
 * also, it provides accessing methods to those buffered images for others.
 */
public class ImageLoader {
	
	private ArrayList<ImageStructure> imagesList;
	
	/*
	 * Constructor
	 */
	public ImageLoader() {
		imagesList = new ArrayList<ImageStructure>();
		
		/*
		 * Load Images of menu
		 */
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_PANEL_MENU_BACKGROUND, loadImage(ImageStructure.IMAGE_NAME_PANEL_MENU_BACKGROUND)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_MENU_1, loadImage(ImageStructure.IMAGE_NAME_MENU_1)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_MENU_2, loadImage(ImageStructure.IMAGE_NAME_MENU_2)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_MENU_3, loadImage(ImageStructure.IMAGE_NAME_MENU_3)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_MENU_4, loadImage(ImageStructure.IMAGE_NAME_MENU_4)));
		
		/*
		 * Load images of blocks
		 */
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BASE, loadImage(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BASE)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BOTTOM_LEFT, loadImage(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BOTTOM_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BOTTOM_RIGHT, loadImage(ImageStructure.IMAGE_NAME_BLOCK_ROAD_BOTTOM_RIGHT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_ROAD_TOP_LEFT, loadImage(ImageStructure.IMAGE_NAME_BLOCK_ROAD_TOP_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_ROAD_TOP_RIGHT, loadImage(ImageStructure.IMAGE_NAME_BLOCK_ROAD_TOP_RIGHT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_FIELD_1, loadImage(ImageStructure.IMAGE_NAME_BLOCK_FIELD_1)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_FIELD_2, loadImage(ImageStructure.IMAGE_NAME_BLOCK_FIELD_2)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_WATER_1, loadImage(ImageStructure.IMAGE_NAME_BLOCK_WATER_1)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_WATER_2, loadImage(ImageStructure.IMAGE_NAME_BLOCK_WATER_2)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_WATER_3, loadImage(ImageStructure.IMAGE_NAME_BLOCK_WATER_3)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_MOUNTAIN, loadImage(ImageStructure.IMAGE_NAME_BLOCK_MOUNTAIN)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BLOCK_FOREST, loadImage(ImageStructure.IMAGE_NAME_BLOCK_FOREST)));
		
		/*
		 * Load images of units
		 */
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_TANK_1_UP, loadImage(ImageStructure.IMAGE_NAME_UNIT_TANK_1_UP)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_TANK_1_DOWN, loadImage(ImageStructure.IMAGE_NAME_UNIT_TANK_1_DOWN)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_TANK_1_LEFT, loadImage(ImageStructure.IMAGE_NAME_UNIT_TANK_1_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_TANK_1_RIGHT, loadImage(ImageStructure.IMAGE_NAME_UNIT_TANK_1_RIGHT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_TANK_2_UP, loadImage(ImageStructure.IMAGE_NAME_UNIT_TANK_2_UP)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_TANK_2_DOWN, loadImage(ImageStructure.IMAGE_NAME_UNIT_TANK_2_DOWN)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_TANK_2_LEFT, loadImage(ImageStructure.IMAGE_NAME_UNIT_TANK_2_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_TANK_2_RIGHT, loadImage(ImageStructure.IMAGE_NAME_UNIT_TANK_2_RIGHT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_1_UP, loadImage(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_1_UP)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_1_DOWN, loadImage(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_1_DOWN)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_1_LEFT, loadImage(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_1_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_1_RIGHT, loadImage(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_1_RIGHT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_2_UP, loadImage(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_2_UP)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_2_DOWN, loadImage(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_2_DOWN)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_2_LEFT, loadImage(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_2_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_2_RIGHT, loadImage(ImageStructure.IMAGE_NAME_UNIT_BATTLESHIP_2_RIGHT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_JET_1_UP, loadImage(ImageStructure.IMAGE_NAME_UNIT_JET_1_UP)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_JET_1_DOWN, loadImage(ImageStructure.IMAGE_NAME_UNIT_JET_1_DOWN)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_JET_1_LEFT, loadImage(ImageStructure.IMAGE_NAME_UNIT_JET_1_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_JET_1_RIGHT, loadImage(ImageStructure.IMAGE_NAME_UNIT_JET_1_RIGHT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_JET_2_UP, loadImage(ImageStructure.IMAGE_NAME_UNIT_JET_2_UP)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_JET_2_DOWN, loadImage(ImageStructure.IMAGE_NAME_UNIT_JET_2_DOWN)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_JET_2_LEFT, loadImage(ImageStructure.IMAGE_NAME_UNIT_JET_2_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_JET_2_RIGHT, loadImage(ImageStructure.IMAGE_NAME_UNIT_JET_2_RIGHT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_INFANTRY_1, loadImage(ImageStructure.IMAGE_NAME_UNIT_INFANTRY_1)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_INFANTRY_2, loadImage(ImageStructure.IMAGE_NAME_UNIT_INFANTRY_2)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_MOBILEAA_1, loadImage(ImageStructure.IMAGE_NAME_UNIT_MOBILEAA_1)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_UNIT_MOBILEAA_2, loadImage(ImageStructure.IMAGE_NAME_UNIT_MOBILEAA_2)));
		
		/*
		 * Load images of cursors
		 */
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_CURSOR_LEFT, loadImage(ImageStructure.IMAGE_NAME_CURSOR_LEFT)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_CURSOR_RIGHT, loadImage(ImageStructure.IMAGE_NAME_CURSOR_RIGHT)));
		
		/*
		 * Others
		 */
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BOARD_INFO, loadImage(ImageStructure.IMAGE_NAME_BOARD_INFO)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_BUTTON_END_TURN, loadImage(ImageStructure.IMAGE_NAME_BUTTON_END_TURN)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_HIGHLIGHT_UNIT_1, loadImage(ImageStructure.IMAGE_NAME_HIGHLIGHT_UNIT_1)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_HIGHLIGHT_UNIT_2, loadImage(ImageStructure.IMAGE_NAME_HIGHLIGHT_UNIT_2)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_MAPIMAGE_1, loadImage(ImageStructure.IMAGE_NAME_MAPIMAGE_1)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_MAPIMAGE_2, loadImage(ImageStructure.IMAGE_NAME_MAPIMAGE_2)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_MAPIMAGE_3, loadImage(ImageStructure.IMAGE_NAME_MAPIMAGE_3)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_MAPIMAGE_4, loadImage(ImageStructure.IMAGE_NAME_MAPIMAGE_4)));
		
		/*
		 * Explosion effect
		 */
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_EXPLOSION_1, loadImage(ImageStructure.IMAGE_NAME_EXPLOSION_1)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_EXPLOSION_2, loadImage(ImageStructure.IMAGE_NAME_EXPLOSION_2)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_EXPLOSION_3, loadImage(ImageStructure.IMAGE_NAME_EXPLOSION_3)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_EXPLOSION_4, loadImage(ImageStructure.IMAGE_NAME_EXPLOSION_4)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_EXPLOSION_5, loadImage(ImageStructure.IMAGE_NAME_EXPLOSION_5)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_EXPLOSION_6, loadImage(ImageStructure.IMAGE_NAME_EXPLOSION_6)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_EXPLOSION_7, loadImage(ImageStructure.IMAGE_NAME_EXPLOSION_7)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_EXPLOSION_8, loadImage(ImageStructure.IMAGE_NAME_EXPLOSION_8)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_1, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_1)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_2, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_2)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_3, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_3)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_4, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_4)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_5, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_5)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_6, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_6)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_7, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_7)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_8, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_8)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_9, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_9)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_10, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_10)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_11, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_11)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_12, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_12)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_13, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_13)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_14, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_14)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_15, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_15)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_16, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_16)));
		imagesList.add(new ImageStructure(ImageStructure.IMAGE_NAME_DESTROYED_17, loadImage(ImageStructure.IMAGE_NAME_DESTROYED_17)));

		
	}
	
	private BufferedImage loadImage(String imagePath) {
		BufferedImage image = null;
		try {
			image = ImageIO.read(new File(imagePath));
		} catch(Exception ex) {
			System.out.println("Fail to load image... " + imagePath);
		}
		return image;
	}
	
	/*
	 * Accessors
	 */
	public BufferedImage getImageByName(String imageName) {
		for(int i = 0; i < imagesList.size(); i++) {
			if(imagesList.get(i).getImageName().equals(imageName))
				return imagesList.get(i).getImage();
		}
		return null;
	}
}
